package 数组.二分查找.有效的完全平方数;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/1/11 14:14
 * @description:给你一个正整数 num 。如果 num 是一个完全平方数，则返回 true ，否则返回 false
 * @version:
 */
public class Solution {
    public static void main(String[] args) {
        System.out.println(isPerfectSquare(9));
    }

    //二分查找 还可以使用快慢指针
    public static boolean isPerfectSquare(int num) {
        int left = 0;
        int right = num;
        while (left <= right){
            int mid = left + (right - left) / 2;
            long l = (long) mid * mid;
            if ( l == num){
                return true;
            }else if (l > num){
                right = mid - 1;
            }else {
                left = left + 1;
            }
        }
        return false;
    }


}
